package com.acme.bankapp.service.email;

import com.acme.bankapp.domain.bank.Client;
import com.acme.bankapp.domain.email.*;
import com.acme.bankapp.domain.email.impl.EmailImpl;
import com.acme.bankapp.domain.email.impl.QueueImpl;

public class EmailService implements Runnable {

	private Queue queue = new QueueImpl();

	private static EmailService instance = new EmailService();

	public static EmailService getEmailService() {
		return instance;
	}

	public void sendNotificationEmail(Client client) {
		Email email = new EmailImpl(client);
		try {
			queue.addEmail(email);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void closeQueue() {
		queue.close();
	}

	@Override
	public void run() {
		try {
			while (true) {
				Email email = queue.getEmail();
				if (email != null)
					System.out.println(Thread.currentThread().getName() + " " + email);
				else
					return;
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
